#!/bin/bash

if [ $# -ne 1 ]
then
	echo "Error: incorrect number of arguments."
	echo "Usage: compile-latex-file.sh tex_file"
	exit
fi

script_dir=$(dirname $0)
package_dir=$script_dir/../packages
tex_file=$1
tex_dir=$(dirname $tex_file)

echo
echo %%
echo %% Setup LaTeX
echo %%
echo
type -P pdflatex > /dev/null 2>&1 || { echo "Error: pdflatex not found. Please 
make sure it is installed and is accessible from the PATH."; exit 1; }
echo pdflatex found

# Set up environment so LaTeX can find necessary packages
export TEXINPUTS="$package_dir//:"
echo pdflatex configured
echo
echo %%
echo %% Compiling LaTeX source \"$tex_file\"
echo %%
echo 
pdflatex -output-directory=$tex_dir -interaction=nonstopmode $tex_file